package com.bhs.sansong.ui.setting;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;

import com.bhs.sansong.BuildConfig;
import com.bhs.sansong.R;
import com.bhs.sansong.base.AppManager;
import com.bhs.sansong.base.BaseActivity;

import com.bhs.sansong.ui.login.PhoneNumberLoginActivity;
import com.bhs.sansong.ui.mine.PersonalInformationActivity;
import com.bhs.sansong.utils.CacheDataManager;
import com.bhs.sansong.utils.SharedPreferencesUtils;
import com.bhs.sansong.utils.ToastUtil;

/**
 * 设置
 */
public class SettingActivity extends BaseActivity implements View.OnClickListener {

    private TextView mTvCacheSize;
    private TextView mTvRealName;
    private boolean is_approve;

    @Override
    public int initLayout() {
        return R.layout.activity_setting;
    }

    @Override
    public void initView() {
        TextView mTvTitle = findViewById(R.id.tv_title);
        mTvTitle.setText("设置");
        findViewById(R.id.btn_log_out).setOnClickListener(this);
        findViewById(R.id.ll_personalInformation).setOnClickListener(this);
        findViewById(R.id.ll_real_name).setOnClickListener(this);
        findViewById(R.id.ll_legislation).setOnClickListener(this);
        findViewById(R.id.ll_account_security).setOnClickListener(this);
        findViewById(R.id.ll_message_setting).setOnClickListener(this);
        findViewById(R.id.ll_about_Us).setOnClickListener(this);
        findViewById(R.id.ll_account_credited).setOnClickListener(this);
        TextView mTvVersion = findViewById(R.id.tv_version);
        mTvVersion.setText(String.format("V%s", BuildConfig.VERSION_NAME));
        findViewById(R.id.ll_cache).setOnClickListener(this);
        mTvCacheSize = findViewById(R.id.tv_cache_size);
        mTvRealName = findViewById(R.id.tv_real_name);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void initData() {
        try {
            mTvCacheSize.setText(CacheDataManager.getTotalCacheSize(this) + "M");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        is_approve = SharedPreferencesUtils.getBoolean(mContext, "is_approve", false);
        if (is_approve) {
            mTvRealName.setTextColor(Color.parseColor("#485EF4"));
            mTvRealName.setText("已实名");
        } else {
            mTvRealName.setTextColor(Color.parseColor("#FF6067"));
            mTvRealName.setText("未实名");
        }
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_log_out:
                SharedPreferencesUtils.clear(mContext, "token");
                AppManager.getInstance().finishAllActivity();
                startActivity(new Intent(mActivity, PhoneNumberLoginActivity.class));
                break;
            case R.id.ll_personalInformation:
                startActivity(new Intent(mActivity, PersonalInformationActivity.class));
                break;
            case R.id.ll_account_security:
                startActivity(new Intent(mActivity, AccountSecurityActivity.class));
                break;
            case R.id.ll_message_setting:
                startActivity(new Intent(mActivity, MessageSetActivity.class));
                break;
            case R.id.ll_about_Us:
                startActivity(new Intent(mActivity, AboutUsActivity.class));
                break;
            case R.id.ll_legislation:
                startActivity(new Intent(mActivity, SecondaryPageActivity.class));
                break;
            case R.id.ll_cache:
                loadingPopupShow();
                CacheDataManager.clearAllCache(this);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        loadingPopupDismiss();
                        //要延时的程序
                        ToastUtil.show("清除成功");
                        mTvCacheSize.setText("0.0M");
                    }
                }, 1000);
                break;
            case R.id.ll_real_name:
                if (!is_approve) {
                    startActivity(new Intent(mActivity, RealNameAuthenticationActivity.class));
                }
                break;
            case R.id.ll_account_credited:
                startActivity(new Intent(mActivity,AccountCreditedActivity.class));
                break;
        }
    }
}